home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / unexec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-25  |  31.2 KB  |  1,155 lines

  1. /* Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994
  2.    Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: FSF 19.28. */
  21.  
  22. /*
  23.  * unexec.c - Convert a running program into an a.out file.
  24.  *
  25.  * Author:    Spencer W. Thomas
  26.  *         Computer Science Dept.
  27.  *         University of Utah
  28.  * Date:    Tue Mar  2 1982
  29.  * Modified heavily since then.
  30.  *
  31.  * Synopsis:
  32.  *    unexec (new_name, a_name, data_start, bss_start, entry_address)
  33.  *    char *new_name, *a_name;
  34.  *    unsigned data_start, bss_start, entry_address;
  35.  *
  36.  * Takes a snapshot of the program and makes an a.out format file in the
  37.  * file named by the string argument new_name.
  38.  * If a_name is non-NULL, the symbol table will be taken from the given file.
  39.  * On some machines, an existing a_name file is required.
  40.  *
  41.  * The boundaries within the a.out file may be adjusted with the data_start
  42.  * and bss_start arguments.  Either or both may be given as 0 for defaults.
  43.  *
  44.  * Data_start gives the boundary between the text segment and the data
  45.  * segment of the program.  The text segment can contain shared, read-only
  46.  * program code and literal data, while the data segment is always unshared
  47.  * and unprotected.  Data_start gives the lowest unprotected address.
  48.  * The value you specify may be rounded down to a suitable boundary
  49.  * as required by the machine you are using.
  50.  *
  51.  * Specifying zero for data_start means the boundary between text and data
  52.  * should not be the same as when the program was loaded.
  53.  * If NO_REMAP is defined, the argument data_start is ignored and the
  54.  * segment boundaries are never changed.
  55.  *
  56.  * Bss_start indicates how much of the data segment is to be saved in the
  57.  * a.out file and restored when the program is executed.  It gives the lowest
  58.  * unsaved address, and is rounded up to a page boundary.  The default when 0
  59.  * is given assumes that the entire data segment is to be stored, including
  60.  * the previous data and bss as well as any additional storage allocated with
  61.  * break (2).
  62.  *
  63.  * The new file is set up to start at entry_address.
  64.  *
  65.  * If you make improvements I'd like to get them too.
  66.  * harpo!utah-cs!thomas, thomas@Utah-20
  67.  *
  68.  */
  69.  
  70. /* Modified to support SysVr3 shared libraries by James Van Artsdalen
  71.  * of Dell Computer Corporation.  james@bigtex.cactus.org.
  72.  */
  73.  
  74. /* There are several compilation parameters affecting unexec:
  75.  
  76. * COFF
  77.  
  78. Define this if your system uses COFF for executables.
  79.  
  80. * COFF_ENCAPSULATE
  81.  
  82. Define this if you are using the GNU coff encapsulated a.out format.
  83. This is closer to a.out than COFF. You should *not* define COFF if
  84. you define COFF_ENCAPSULATE
  85.  
  86. Otherwise we assume you use Berkeley format.
  87.  
  88. * NO_REMAP
  89.  
  90. Define this if you do not want to try to save Emacs's pure data areas
  91. as part of the text segment.
  92.  
  93. Saving them as text is good because it allows users to share more.
  94.  
  95. However, on machines that locate the text area far from the data area,
  96. the boundary cannot feasibly be moved.  Such machines require
  97. NO_REMAP.
  98.  
  99. Also, remapping can cause trouble with the built-in startup routine
  100. /lib/crt0.o, which defines `environ' as an initialized variable.
  101. Dumping `environ' as pure does not work!  So, to use remapping,
  102. you must write a startup routine for your machine in Emacs's crt0.c.
  103. If NO_REMAP is defined, Emacs uses the system's crt0.o.
  104.  
  105. * SECTION_ALIGNMENT
  106.  
  107. Some machines that use COFF executables require that each section
  108. start on a certain boundary *in the COFF file*.  Such machines should
  109. define SECTION_ALIGNMENT to a mask of the low-order bits that must be
  110. zero on such a boundary.  This mask is used to control padding between
  111. segments in the COFF file.
  112.  
  113. If SECTION_ALIGNMENT is not defined, the segments are written
  114. consecutively with no attempt at alignment.  This is right for
  115. unmodified system V.
  116.  
  117. * SEGMENT_MASK
  118.  
  119. Some machines require that the beginnings and ends of segments
  120. *in core* be on certain boundaries.  For most machines, a page
  121. boundary is sufficient.  That is the default.  When a larger
  122. boundary is needed, define SEGMENT_MASK to a mask of
  123. the bits that must be zero on such a boundary.
  124.  
  125. * A_TEXT_OFFSET(HDR)
  126.  
  127. Some machines count the a.out header as part of the size of the text
  128. segment (a_text); they may actually load the header into core as the
  129. first data in the text segment.  Some have additional padding between
  130. the header and the real text of the program that is counted in a_text.
  131.  
  132. For these machines, define A_TEXT_OFFSET(HDR) to examine the header
  133. structure HDR and return the number of bytes to add to `a_text'
  134. before writing it (above and beyond the number of bytes of actual
  135. program text).  HDR's standard fields are already correct, except that
  136. this adjustment to the `a_text' field has not yet been made;
  137. thus, the amount of offset can depend on the data in the file.
  138.   
  139. * A_TEXT_SEEK(HDR)
  140.  
  141. If defined, this macro specifies the number of bytes to seek into the
  142. a.out file before starting to write the text segment.
  143.  
  144. * EXEC_MAGIC
  145.  
  146. For machines using COFF, this macro, if defined, is a value stored
  147. into the magic number field of the output file.
  148.  
  149. * ADJUST_EXEC_HEADER
  150.  
  151. This macro can be used to generate statements to adjust or
  152. initialize nonstandard fields in the file header
  153.  
  154. * ADDR_CORRECT(ADDR)
  155.  
  156. Macro to correct an int which is the bit pattern of a pointer to a byte
  157. into an int which is the number of a byte.
  158.  
  159. This macro has a default definition which is usually right.
  160. This default definition is a no-op on most machines (where a
  161. pointer looks like an int) but not on all machines.
  162.  
  163. */
  164.  
  165. #ifndef emacs
  166. #define PERROR(arg) perror (arg); return -1
  167. #else
  168. #define IN_UNEXEC
  169. #define DONT_ENCAPSULATE /* we include lisp.h so we want to make sure we
  170.                 don't get filename conversion.  The caller
  171.                 of unexec() is assumed to have done this
  172.                 already (it's easier to do it this way than
  173.                 to modify all the unexec modules to ensure
  174.                 that all weirdo functions, such as
  175.                 elf_write_modified_data(), have proper
  176.                 filename conversion applied). */
  177. #include <config.h>
  178. #define PERROR(file) report_error (file, new)
  179. #endif
  180.  
  181. #if __STDC__ || defined(STDC_HEADERS)
  182.  
  183. /* I don't know how correct this attempt to get more prototypes is... */
  184. # if defined(sun) && defined(_POSIX_SOURCE)
  185. #  undef _POSIX_SOURCE
  186. # endif
  187.  
  188. # if defined(__lucid) && !defined(__STDC_EXTENDED__)
  189. #  define __STDC_EXTENDED__ 1
  190. # endif
  191.  
  192. # include <stdlib.h>
  193. # include <unistd.h>
  194. # include <string.h>
  195.  
  196. # ifdef __lucid
  197. #  include <sysent.h>
  198. # endif
  199.  
  200. #endif
  201.  
  202. /* I don't understand this, but it's necessary to get some slots in struct exec
  203.    from /usr/include/sys/exec.h when running LCC in strict ANSI mode.  We don't
  204.    need this in K&R mode...
  205.  */
  206. #if defined(__lucid) && defined(__sparc) && !defined(sun)
  207. # define sun 1
  208. #endif
  209.  
  210. #ifndef CANNOT_DUMP  /* all rest of file!  */
  211.  
  212. #ifndef CANNOT_UNEXEC /* most of rest of file */
  213.  
  214. #ifdef COFF_ENCAPSULATE
  215. int need_coff_header = 1;
  216. #include <coff-encap/a.out.encap.h> /* The location might be a poor assumption */
  217. #else
  218. #ifdef MSDOS
  219. #include <coff.h>
  220. #define filehdr external_filehdr
  221. #define scnhdr external_scnhdr
  222. #define syment external_syment
  223. #define auxent external_auxent
  224. #define n_numaux e_numaux
  225. #define n_type e_type
  226. struct aouthdr
  227. {
  228.   unsigned short    magic;    /* type of file                */
  229.   unsigned short    vstamp;    /* version stamp            */
  230.   unsigned long        tsize;    /* text size in bytes, padded to FW bdry*/
  231.   unsigned long        dsize;    /* initialized data "  "        */
  232.   unsigned long        bsize;    /* uninitialized data "   "        */
  233.   unsigned long        entry;    /* entry pt.                */
  234.   unsigned long         text_start;/* base of text used for this file */
  235.   unsigned long         data_start;/* base of data used for this file */
  236. };
  237.  
  238.  
  239. #else /* not MSDOS */
  240. #include <a.out.h>
  241. #endif /* not MSDOS */
  242. #endif /* not COFF_ENCAPSULATE */
  243.  
  244. /* Define getpagesize () if the system does not.
  245.    Note that this may depend on symbols defined in a.out.h
  246.  */
  247. #include "getpagesize.h"
  248.  
  249. #ifndef makedev            /* Try to detect types.h already loaded */
  250. #include <sys/types.h>
  251. #endif /* makedev */
  252. #include <stdio.h>
  253. #include <sys/stat.h>
  254. #include <errno.h>
  255.  
  256. #include <sys/file.h>    /* Must be after sys/types.h for USG and BSD4_1*/
  257.  
  258. #ifdef USG5
  259. #include <fcntl.h>
  260. #endif
  261.  
  262. #ifndef O_RDONLY
  263. #define O_RDONLY 0
  264. #endif
  265. #ifndef O_RDWR
  266. #define O_RDWR 2
  267. #endif
  268.  
  269.  
  270. extern char *start_of_text ();        /* Start of text */
  271.  
  272. extern void *start_of_data ();        /* Start of initialized data */
  273.  
  274. #ifdef COFF
  275. static long block_copy_start;        /* Old executable start point */
  276. static struct filehdr f_hdr;        /* File header */
  277. static struct aouthdr f_ohdr;        /* Optional file header (a.out) */
  278. long bias;            /* Bias to add for growth */
  279. long lnnoptr;            /* Pointer to line-number info within file */
  280. #define SYMS_START block_copy_start
  281.  
  282. static long text_scnptr;
  283. static long data_scnptr;
  284.  
  285. #else /* not COFF */
  286.  
  287. #ifdef __STDC__
  288. #ifndef __sys_stdtypes_h
  289. #ifndef _PTRDIFF_T
  290. typedef long ptrdiff_t;
  291. #endif
  292. #endif
  293. #ifndef HPUX
  294. /* not sure where this for NetBSD should really go
  295.    and it probably applies to other systems */
  296. #if !defined(__NetBSD__) && !defined(__bsdi__)
  297. extern void *sbrk (ptrdiff_t);
  298. #else
  299. extern char *sbrk ();
  300. #endif /* __NetBSD__ */
  301. #endif /* HPUX */
  302. #else
  303. extern void *sbrk ();
  304. #endif
  305.  
  306. #define SYMS_START ((long) N_SYMOFF (ohdr))
  307.  
  308. /* Some machines override the structure name for an a.out header.  */
  309. #ifndef EXEC_HDR_TYPE
  310. #define EXEC_HDR_TYPE struct exec
  311. #endif
  312.  
  313. #ifdef HPUX
  314. #ifdef HP9000S200_ID
  315. #define MY_ID HP9000S200_ID
  316. #else
  317. #include <model.h>
  318. #define MY_ID MYSYS
  319. #endif /* no HP9000S200_ID */
  320. static MAGIC OLDMAGIC = {MY_ID, SHARE_MAGIC};
  321. static MAGIC NEWMAGIC = {MY_ID, DEMAND_MAGIC};
  322. #define N_TXTOFF(x) TEXT_OFFSET(x)
  323. #define N_SYMOFF(x) LESYM_OFFSET(x)
  324. static EXEC_HDR_TYPE hdr, ohdr;
  325.  
  326. #else /* not HPUX */
  327.  
  328. #if defined (USG) && !defined (IBMAIX) && !defined (IRIS) && !defined (COFF_ENCAPSULATE) && !defined (LINUX)
  329. static struct bhdr hdr, ohdr;
  330. #define a_magic fmagic
  331. #define a_text tsize
  332. #define a_data dsize
  333. #define a_bss bsize
  334. #define a_syms ssize
  335. #define a_trsize rtsize
  336. #define a_drsize rdsize
  337. #define a_entry entry
  338. #define    N_BADMAG(x) \
  339.     (((x).fmagic)!=OMAGIC && ((x).fmagic)!=NMAGIC &&\
  340.      ((x).fmagic)!=FMAGIC && ((x).fmagic)!=IMAGIC)
  341. #define NEWMAGIC FMAGIC
  342. #else /* IRIS or IBMAIX or not USG */
  343. static EXEC_HDR_TYPE hdr, ohdr;
  344. #define NEWMAGIC ZMAGIC
  345. #endif /* IRIS or IBMAIX not USG */
  346. #endif /* not HPUX */
  347.  
  348. static int unexec_text_start;
  349. static int unexec_data_start;
  350.  
  351. #ifdef COFF_ENCAPSULATE
  352. /* coffheader is defined in the GNU a.out.encap.h file.  */
  353. struct coffheader coffheader;
  354. #endif
  355.  
  356. #endif /* not COFF */
  357.  
  358. static int pagemask;
  359.  
  360. /* Correct an int which is the bit pattern of a pointer to a byte
  361.    into an int which is the number of a byte.
  362.    This is a no-op on ordinary machines, but not on all.  */
  363.  
  364. #ifndef ADDR_CORRECT   /* Let m-*.h files override this definition */
  365. #define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
  366. #endif
  367.  
  368. #ifdef emacs
  369.  
  370. #include "lisp.h"
  371.  
  372. static void
  373. report_error (file, fd)
  374.      CONST char *file;
  375.      int fd;
  376. {
  377.   if (fd)
  378.     close (fd);
  379.   report_file_error ("Cannot unexec", Fcons (build_ext_string (file), Qnil));
  380. }
  381. #endif /* emacs */
  382.  
  383. #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
  384. #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
  385. #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
  386.  
  387. static void
  388. report_error_1 (fd, msg, a1, a2)
  389.      int fd;
  390.      CONST char *msg;
  391.      int a1, a2;
  392. {
  393.   close (fd);
  394. #ifdef emacs
  395.   error (msg, a1, a2);
  396. #else
  397.   fprintf (stderr, msg, a1, a2);
  398.   fprintf (stderr, "\n");
  399. #endif
  400. }
  401.  
  402. static int make_hdr (int new, int a_out, unsigned data_start,
  403.              unsigned bss_start, unsigned entry_address,
  404.              char *a_name, char *new_name);
  405. static int copy_text_and_data (int new, int a_out);
  406. static int copy_sym (int new, int a_out, char *a_name, char *new_name);
  407. static void mark_x (char *name);
  408.  
  409. /* ****************************************************************
  410.  * unexec
  411.  *
  412.  * driving logic.
  413.  */
  414. int
  415. unexec (new_name, a_name, data_start, bss_start, entry_address)
  416.      char *new_name, *a_name;
  417.      unsigned data_start, bss_start, entry_address;
  418. {
  419.   int new, a_out = -1;
  420.  
  421.   if (a_name && (a_out = open (a_name, O_RDONLY)) < 0)
  422.     {
  423.       PERROR (a_name);
  424.     }
  425.   if ((new = creat (new_name, 0666)) < 0)
  426.     {
  427.       PERROR (new_name);
  428.     }
  429.  
  430.   if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0
  431.       || copy_text_and_data (new, a_out) < 0
  432.       || copy_sym (new, a_out, a_name, new_name) < 0
  433. #ifdef COFF
  434. #ifndef COFF_BSD_SYMBOLS
  435.       || adjust_lnnoptrs (new, a_out, new_name) < 0
  436. #endif
  437. #endif
  438.       )
  439.     {
  440.       close (new);
  441.       /* unlink (new_name);            / * Failed, unlink new a.out */
  442.       return -1;    
  443.     }
  444.  
  445.   close (new);
  446.   if (a_out >= 0)
  447.     close (a_out);
  448.   mark_x (new_name);
  449.   return 0;
  450. }
  451.  
  452. /* ****************************************************************
  453.  * make_hdr
  454.  *
  455.  * Make the header in the new a.out from the header in core.
  456.  * Modify the text and data sizes.
  457.  */
  458. static int
  459. make_hdr (int new, int a_out, unsigned data_start, unsigned bss_start,
  460.       unsigned entry_address, char *a_name, char *new_name)
  461. {
  462. #ifdef COFF
  463.   auto struct scnhdr f_thdr;        /* Text section header */
  464.   auto struct scnhdr f_dhdr;        /* Data section header */
  465.   auto struct scnhdr f_bhdr;        /* Bss section header */
  466.   auto struct scnhdr scntemp;        /* Temporary section header */
  467.   int scns;
  468. #endif /* COFF */
  469. #ifdef USG_SHARED_LIBRARIES
  470.   extern unsigned int bss_end;
  471. #else
  472.   unsigned int bss_end;
  473. #endif
  474.  
  475.   pagemask = getpagesize () - 1;
  476.  
  477.   /* Adjust text/data boundary. */
  478. #ifdef NO_REMAP
  479.   data_start = (int) start_of_data ();
  480. #else /* not NO_REMAP */
  481.   if (!data_start)
  482.     data_start = (int) start_of_data ();
  483. #endif /* not NO_REMAP */
  484.   data_start = ADDR_CORRECT (data_start);
  485.  
  486. #ifdef SEGMENT_MASK
  487.   data_start = data_start & ~SEGMENT_MASK; /* (Down) to segment boundary. */
  488. #else
  489.   data_start = data_start & ~pagemask; /* (Down) to page boundary. */
  490. #endif
  491.  
  492.   bss_end = ADDR_CORRECT (sbrk (0)) + pagemask;
  493.   bss_end &= ~ pagemask;
  494.  
  495.   /* Adjust data/bss boundary. */
  496.   if (bss_start != 0)
  497.     {
  498.       bss_start = (ADDR_CORRECT (bss_start) + pagemask);
  499.       /* (Up) to page bdry. */
  500.       bss_start &= ~ pagemask;
  501.       if (bss_start > bss_end)
  502.     {
  503.       ERROR1 ("unexec: Specified bss_start (%u) is past end of program",
  504.           bss_start);
  505.     }
  506.     }
  507.   else
  508.     bss_start = bss_end;
  509.  
  510.   if (data_start > bss_start)    /* Can't have negative data size. */
  511.     {
  512.       ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
  513.           data_start, bss_start);
  514.     }
  515.  
  516. #ifdef COFF
  517.   /* Salvage as much info from the existing file as possible */
  518.   if (a_out >= 0)
  519.     {
  520.       if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
  521.     {
  522.       PERROR (a_name);
  523.     }
  524.       block_copy_start += sizeof (f_hdr);
  525.       if (f_hdr.f_opthdr > 0)
  526.     {
  527.       if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
  528.         {
  529.           PERROR (a_name);
  530.         }
  531.       block_copy_start += sizeof (f_ohdr);
  532.     }
  533.       /* Loop through section headers, copying them in */
  534.       lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0);
  535.       for (scns = f_hdr.f_nscns; scns > 0; scns--) {
  536.     if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
  537.       {
  538.         PERROR (a_name);
  539.       }
  540.     if (scntemp.s_scnptr > 0L)
  541.       {
  542.             if (block_copy_start < scntemp.s_scnptr + scntemp.s_size)
  543.           block_copy_start = scntemp.s_scnptr + scntemp.s_size;
  544.       }
  545.     if (strcmp (scntemp.s_name, ".text") == 0)
  546.       {
  547.         f_thdr = scntemp;
  548.       }
  549.     else if (strcmp (scntemp.s_name, ".data") == 0)
  550.       {
  551.         f_dhdr = scntemp;
  552.       }
  553.     else if (strcmp (scntemp.s_name, ".bss") == 0)
  554.       {
  555.         f_bhdr = scntemp;
  556.       }
  557.       }
  558.     }
  559.   else
  560.     {
  561.       ERROR0 ("can't build a COFF file from scratch yet");
  562.     }
  563.  
  564.   /* Now we alter the contents of all the f_*hdr variables
  565.      to correspond to what we want to dump.  */
  566.  
  567. #ifdef USG_SHARED_LIBRARIES
  568.  
  569.   /* The amount of data we're adding to the file is distance from the
  570.    * end of the original .data space to the current end of the .data
  571.    * space.
  572.    */
  573.  
  574.   bias = bss_start - (f_ohdr.data_start + f_dhdr.s_size);
  575.  
  576. #endif
  577.  
  578.   f_hdr.f_flags |= (F_RELFLG | F_EXEC);
  579. #ifdef TPIX
  580.   f_hdr.f_nscns = 3;
  581. #endif
  582. #ifdef EXEC_MAGIC
  583.   f_ohdr.magic = EXEC_MAGIC;
  584. #endif
  585. #ifndef NO_REMAP
  586.   f_ohdr.text_start = (long) start_of_text ();
  587.   f_ohdr.tsize = data_start - f_ohdr.text_start;
  588.   f_ohdr.data_start = data_start;
  589. #endif /* NO_REMAP */
  590.   f_ohdr.dsize = bss_start - f_ohdr.data_start;
  591.   f_ohdr.bsize = bss_end - bss_start;
  592. #ifndef KEEP_OLD_TEXT_SCNPTR
  593.   /* On some machines, the old values are right.
  594.      ??? Maybe on all machines with NO_REMAP.  */
  595.   f_thdr.s_size = f_ohdr.tsize;
  596.   f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr);
  597.   f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr));
  598. #endif /* KEEP_OLD_TEXT_SCNPTR */
  599. #ifdef ADJUST_TEXT_SCNHDR_SIZE
  600.   /* On some machines, `text size' includes all headers.  */
  601.   f_thdr.s_size -= f_thdr.s_scnptr;
  602. #endif /* ADJUST_TEST_SCNHDR_SIZE */
  603.   lnnoptr = f_thdr.s_lnnoptr;
  604. #ifdef SECTION_ALIGNMENT
  605.   /* Some systems require special alignment
  606.      of the sections in the file itself.  */
  607.   f_thdr.s_scnptr
  608.     = (f_thdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT;
  609. #endif /* SECTION_ALIGNMENT */
  610. #ifdef TPIX
  611.   f_thdr.s_scnptr = 0xd0;
  612. #endif
  613.   text_scnptr = f_thdr.s_scnptr;
  614. #ifdef ADJUST_TEXTBASE
  615.   text_scnptr = sizeof (f_hdr) + sizeof (f_ohdr) + (f_hdr.f_nscns) * (sizeof (f_thdr));
  616. #endif
  617. #ifndef KEEP_OLD_PADDR
  618.   f_dhdr.s_paddr = f_ohdr.data_start;
  619. #endif /* KEEP_OLD_PADDR */
  620.   f_dhdr.s_vaddr = f_ohdr.data_start;
  621.   f_dhdr.s_size = f_ohdr.dsize;
  622.   f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size;
  623. #ifdef SECTION_ALIGNMENT
  624.   /* Some systems require special alignment
  625.      of the sections in the file itself.  */
  626.   f_dhdr.s_scnptr
  627.     = (f_dhdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT;
  628. #endif /* SECTION_ALIGNMENT */
  629. #ifdef DATA_SECTION_ALIGNMENT
  630.   /* Some systems require special alignment
  631.      of the data section only.  */
  632.   f_dhdr.s_scnptr
  633.     = (f_dhdr.s_scnptr + DATA_SECTION_ALIGNMENT) & ~DATA_SECTION_ALIGNMENT;
  634. #endif /* DATA_SECTION_ALIGNMENT */
  635.   data_scnptr = f_dhdr.s_scnptr;
  636. #ifndef KEEP_OLD_PADDR
  637.   f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize;
  638. #endif /* KEEP_OLD_PADDR */
  639.   f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
  640.   f_bhdr.s_size = f_ohdr.bsize;
  641.   f_bhdr.s_scnptr = 0L;
  642. #ifndef USG_SHARED_LIBRARIES
  643.   bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
  644. #endif
  645.  
  646.   if (f_hdr.f_symptr > 0L)
  647.     {
  648.       f_hdr.f_symptr += bias;
  649.     }
  650.  
  651.   if (f_thdr.s_lnnoptr > 0L)
  652.     {
  653.       f_thdr.s_lnnoptr += bias;
  654.     }
  655.  
  656. #ifdef ADJUST_EXEC_HEADER
  657.   ADJUST_EXEC_HEADER;
  658. #endif /* ADJUST_EXEC_HEADER */
  659.  
  660.   if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
  661.     {
  662.       PERROR (new_name);
  663.     }
  664.  
  665.   if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
  666.     {
  667.       PERROR (new_name);
  668.     }
  669.  
  670. #ifndef USG_SHARED_LIBRARIES
  671.  
  672.   if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
  673.     {
  674.       PERROR (new_name);
  675.     }
  676.  
  677.   if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
  678.     {
  679.       PERROR (new_name);
  680.     }
  681.  
  682.   if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
  683.     {
  684.       PERROR (new_name);
  685.     }
  686.  
  687. #else /* USG_SHARED_LIBRARIES */
  688.  
  689.   /* The purpose of this code is to write out the new file's section
  690.    * header table.
  691.    *
  692.    * Scan through the original file's sections.  If the encountered
  693.    * section is one we know (.text, .data or .bss), write out the
  694.    * correct header.  If it is a section we do not know (such as
  695.    * .lib), adjust the address of where the section data is in the
  696.    * file, and write out the header.
  697.    *
  698.    * If any section precedes .text or .data in the file, this code
  699.    * will not adjust the file pointer for that section correctly.
  700.    */
  701.  
  702.   /* This used to use sizeof (f_ohdr) instead of .f_opthdr.
  703.      .f_opthdr is said to be right when there is no optional header.  */
  704.   lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0);
  705.  
  706.   for (scns = f_hdr.f_nscns; scns > 0; scns--)
  707.     {
  708.       if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
  709.     PERROR (a_name);
  710.  
  711.       if (!strcmp (scntemp.s_name, f_thdr.s_name))    /* .text */
  712.     {
  713.       if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
  714.         PERROR (new_name);
  715.     }
  716.       else if (!strcmp (scntemp.s_name, f_dhdr.s_name))    /* .data */
  717.     {
  718.       if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
  719.         PERROR (new_name);
  720.     }
  721.       else if (!strcmp (scntemp.s_name, f_bhdr.s_name))    /* .bss */
  722.     {
  723.       if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
  724.         PERROR (new_name);
  725.     }
  726.       else
  727.     {
  728.       if (scntemp.s_scnptr)
  729.         scntemp.s_scnptr += bias;
  730.       if (write (new, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
  731.         PERROR (new_name);
  732.     }
  733.     }
  734. #endif /* USG_SHARED_LIBRARIES */
  735.  
  736.   return (0);
  737.  
  738. #else /* if not COFF */
  739.  
  740.   /* Get symbol table info from header of a.out file if given one. */
  741.   if (a_out >= 0)
  742.     {
  743. #ifdef COFF_ENCAPSULATE
  744.       if (read (a_out, &coffheader, sizeof coffheader) != sizeof coffheader)
  745.     {
  746.       PERROR(a_name);
  747.     }
  748.       if (coffheader.f_magic != COFF_MAGIC)
  749.     {
  750.       ERROR1("%s doesn't have legal coff magic number\n", a_name);
  751.     }
  752. #endif
  753.       if (read (a_out, (char *) &ohdr, sizeof hdr) != sizeof hdr)
  754.     {
  755.       PERROR (a_name);
  756.     }
  757.  
  758.       if (N_BADMAG (ohdr))
  759.     {
  760.       ERROR1 ("invalid magic number in %s", a_name);
  761.     }
  762.       hdr = ohdr;
  763.     }
  764.   else
  765.     {
  766. #ifdef COFF_ENCAPSULATE
  767.       /* We probably could without too much trouble. The code is in gld
  768.        * but I don't have that much time or incentive.
  769.        */
  770.       ERROR0 ("can't build a COFF file from scratch yet");
  771. #else
  772.       memset ((void *)&hdr, 0, sizeof hdr);
  773. #endif
  774.     }
  775.  
  776.   unexec_text_start = (long) start_of_text ();
  777.   unexec_data_start = data_start;
  778.  
  779.   /* Machine-dependent fixup for header, or maybe for unexec_text_start */
  780. #ifdef ADJUST_EXEC_HEADER
  781.   ADJUST_EXEC_HEADER;
  782. #endif /* ADJUST_EXEC_HEADER */
  783.  
  784.   hdr.a_trsize = 0;
  785.   hdr.a_drsize = 0;
  786.   if (entry_address != 0)
  787.     hdr.a_entry = entry_address;
  788.  
  789.   hdr.a_bss = bss_end - bss_start;
  790.   hdr.a_data = bss_start - data_start;
  791. #ifdef NO_REMAP
  792.   hdr.a_text = ohdr.a_text;
  793. #else /* not NO_REMAP */
  794.   hdr.a_text = data_start - unexec_text_start;
  795.  
  796. #ifdef A_TEXT_OFFSET
  797.   hdr.a_text += A_TEXT_OFFSET (ohdr);
  798. #endif
  799.  
  800. #endif /* not NO_REMAP */
  801.  
  802. #ifdef COFF_ENCAPSULATE
  803.   /* We are encapsulating BSD format within COFF format.  */
  804.   {
  805.     struct coffscn *tp, *dp, *bp;
  806.     tp = &coffheader.scns[0];
  807.     dp = &coffheader.scns[1];
  808.     bp = &coffheader.scns[2];
  809.     tp->s_size = hdr.a_text + sizeof(struct exec);
  810.     dp->s_paddr = data_start;
  811.     dp->s_vaddr = data_start;
  812.     dp->s_size = hdr.a_data;
  813.     bp->s_paddr = dp->s_vaddr + dp->s_size;
  814.     bp->s_vaddr = bp->s_paddr;
  815.     bp->s_size = hdr.a_bss;
  816.     coffheader.tsize = tp->s_size;
  817.     coffheader.dsize = dp->s_size;
  818.     coffheader.bsize = bp->s_size;
  819.     coffheader.text_start = tp->s_vaddr;
  820.     coffheader.data_start = dp->s_vaddr;
  821.   }
  822.   if (write (new, &coffheader, sizeof coffheader) != sizeof coffheader)
  823.     {
  824.       PERROR(new_name);
  825.     }
  826. #endif /* COFF_ENCAPSULATE */
  827.  
  828.   if (write (new, (char *) &hdr, sizeof hdr) != sizeof hdr)
  829.     {
  830.       PERROR (new_name);
  831.     }
  832.  
  833. #if 0 /* This #ifndef caused a bug on Linux when using QMAGIC.  */
  834.   /* This adjustment was done above only #ifndef NO_REMAP,
  835.      so only undo it now #ifndef NO_REMAP.  */
  836.   /* #ifndef NO_REMAP  */
  837. #endif
  838. #ifdef A_TEXT_OFFSET
  839.   hdr.a_text -= A_TEXT_OFFSET (ohdr);
  840. #endif
  841.  
  842.   return 0;
  843.  
  844. #endif /* not COFF */
  845. }
  846.  
  847. static void write_segment (int, char *, char *);
  848.  
  849. /* ****************************************************************
  850.  * copy_text_and_data
  851.  *
  852.  * Copy the text and data segments from memory to the new a.out
  853.  */
  854. static int
  855. copy_text_and_data (int new, int a_out)
  856. {
  857.   char *end;
  858.   char *ptr;
  859.  
  860. #ifdef COFF
  861.  
  862. #ifdef USG_SHARED_LIBRARIES
  863.  
  864.   int scns;
  865.   struct scnhdr scntemp;        /* Temporary section header */
  866.  
  867.   /* The purpose of this code is to write out the new file's section
  868.    * contents.
  869.    *
  870.    * Step through the section table.  If we know the section (.text,
  871.    * .data) do the appropriate thing.  Otherwise, if the section has
  872.    * no allocated space in the file (.bss), do nothing.  Otherwise,
  873.    * the section has space allocated in the file, and is not a section
  874.    * we know.  So just copy it.
  875.    */
  876.  
  877.   lseek (a_out, sizeof (struct filehdr) + sizeof (struct aouthdr), 0);
  878.  
  879.   for (scns = f_hdr.f_nscns; scns > 0; scns--)
  880.     {
  881.       if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
  882.     PERROR ("temacs");
  883.  
  884.       if (!strcmp (scntemp.s_name, ".text"))
  885.     {
  886.       lseek (new, (long) text_scnptr, 0);
  887.       ptr = (char *) f_ohdr.text_start;
  888.       end = ptr + f_ohdr.tsize;
  889.       write_segment (new, ptr, end);
  890.     }
  891.       else if (!strcmp (scntemp.s_name, ".data"))
  892.     {
  893.       lseek (new, (long) data_scnptr, 0);
  894.       ptr = (char *) f_ohdr.data_start;
  895.       end = ptr + f_ohdr.dsize;
  896.       write_segment (new, ptr, end);
  897.     }
  898.       else if (!scntemp.s_scnptr)
  899.     ; /* do nothing - no data for this section */
  900.       else
  901.     {
  902.       char page[BUFSIZ];
  903.       int size, n;
  904.       long old_a_out_ptr = lseek (a_out, 0, 1);
  905.  
  906.       lseek (a_out, scntemp.s_scnptr, 0);
  907.       for (size = scntemp.s_size; size > 0; size -= sizeof (page))
  908.         {
  909.           n = size > sizeof (page) ? sizeof (page) : size;
  910.           if (read (a_out, page, n) != n || write (new, page, n) != n)
  911.         PERROR ("emacs");
  912.         }
  913.       lseek (a_out, old_a_out_ptr, 0);
  914.     }
  915.     }
  916.  
  917. #else /* COFF, but not USG_SHARED_LIBRARIES */
  918.  
  919.   lseek (new, (long) text_scnptr, 0);
  920.   ptr = (char *) f_ohdr.text_start;
  921. #ifdef HEADER_INCL_IN_TEXT
  922.   /* For Gould UTX/32, text starts after headers */
  923.   ptr = (char *) (ptr + text_scnptr);
  924. #endif /* HEADER_INCL_IN_TEXT */
  925.   end = ptr + f_ohdr.tsize;
  926.   write_segment (new, ptr, end);
  927.  
  928.   lseek (new, (long) data_scnptr, 0);
  929.   ptr = (char *) f_ohdr.data_start;
  930.   end = ptr + f_ohdr.dsize;
  931.   write_segment (new, ptr, end);
  932.  
  933. #endif /* USG_SHARED_LIBRARIES */
  934.  
  935. #else /* if not COFF */
  936.  
  937. /* Some machines count the header as part of the text segment.
  938.    That is to say, the header appears in core
  939.    just before the address that start_of_text () returns.
  940.    For them, N_TXTOFF is the place where the header goes.
  941.    We must adjust the seek to the place after the header.
  942.    Note that at this point hdr.a_text does *not* count
  943.    the extra A_TEXT_OFFSET bytes, only the actual bytes of code.  */
  944.  
  945. #ifdef A_TEXT_SEEK
  946.   lseek (new, (long) A_TEXT_SEEK (hdr), 0);
  947. #else
  948.   lseek (new, (long) N_TXTOFF (hdr), 0);
  949. #endif /* no A_TEXT_SEEK */
  950.  
  951.   ptr = (char *) unexec_text_start;
  952.   end = ptr + hdr.a_text;
  953.   write_segment (new, ptr, end);
  954.  
  955.   ptr = (char *) unexec_data_start;
  956.   end = ptr + hdr.a_data;
  957. /*  This lseek is certainly incorrect when A_TEXT_OFFSET
  958.     and I believe it is a no-op otherwise.
  959.     Let's see if its absence ever fails.  */
  960. /*  lseek (new, (long) N_TXTOFF (hdr) + hdr.a_text, 0); */
  961.   write_segment (new, ptr, end);
  962.  
  963. #endif /* not COFF */
  964.  
  965.   return 0;
  966. }
  967.  
  968. static void
  969. write_segment (new, ptr, end)
  970.      int new;
  971.      char *ptr, *end;
  972. {
  973.   int i, nwrite, ret;
  974.   char buf[80];
  975.   extern int errno;
  976.   char zeros[128];
  977.  
  978.   memset (zeros, 0, sizeof zeros);
  979.  
  980.   for (i = 0; ptr < end;)
  981.     {
  982.       /* distance to next multiple of 128.  */
  983.       nwrite = (((int) ptr + 128) & -128) - (int) ptr;
  984.       /* But not beyond specified end.  */
  985.       if (nwrite > end - ptr) nwrite = end - ptr;
  986.       ret = write (new, ptr, nwrite);
  987.       /* If write gets a page fault, it means we reached
  988.      a gap between the old text segment and the old data segment.
  989.      This gap has probably been remapped into part of the text segment.
  990.      So write zeros for it.  */
  991.       if (ret == -1
  992. #ifdef EFAULT
  993.       && errno == EFAULT
  994. #endif
  995.       )
  996.     write (new, zeros, nwrite);
  997.       else if (nwrite != ret)
  998.     {
  999.       sprintf (buf,
  1000.            "unexec write failure: addr 0x%lx, fileno %d, size 0x%x, wrote 0x%x, errno %d",
  1001.            (unsigned long) ptr, new, nwrite, ret, errno);
  1002.       PERROR (buf);
  1003.     }
  1004.       i += nwrite;
  1005.       ptr += nwrite;
  1006.     }
  1007. }
  1008.  
  1009. /* ****************************************************************
  1010.  * copy_sym
  1011.  *
  1012.  * Copy the relocation information and symbol table from the a.out to the new
  1013.  */
  1014. static int
  1015. copy_sym (int new, int a_out, char *a_name, char *new_name)
  1016. {
  1017.   char page[1024];
  1018.   int n;
  1019.  
  1020.   if (a_out < 0)
  1021.     return 0;
  1022.  
  1023. #ifdef COFF
  1024.   if (SYMS_START == 0L)
  1025.     return 0;
  1026. #endif  /* COFF */
  1027.  
  1028. #ifdef COFF
  1029.   if (lnnoptr)            /* if there is line number info */
  1030.     lseek (a_out, lnnoptr, 0);    /* start copying from there */
  1031.   else
  1032. #endif /* COFF */
  1033.     lseek (a_out, SYMS_START, 0);    /* Position a.out to symtab. */
  1034.  
  1035.   while ((n = read (a_out, page, sizeof page)) > 0)
  1036.     {
  1037.       if (write (new, page, n) != n)
  1038.     {
  1039.       PERROR (new_name);
  1040.     }
  1041.     }
  1042.   if (n < 0)
  1043.     {
  1044.       PERROR (a_name);
  1045.     }
  1046.   return 0;
  1047. }
  1048.  
  1049. /* ****************************************************************
  1050.  * mark_x
  1051.  *
  1052.  * After successfully building the new a.out, mark it executable
  1053.  */
  1054. static void
  1055. mark_x (char *name)
  1056. {
  1057.   struct stat sbuf;
  1058.   int um;
  1059.   int new = 0;  /* for PERROR */
  1060.  
  1061.   um = umask (777);
  1062.   umask (um);
  1063.   if (stat (name, &sbuf) == -1)
  1064.     {
  1065.       PERROR (name);
  1066.     }
  1067.   sbuf.st_mode |= 0111 & ~um;
  1068.   if (chmod (name, sbuf.st_mode) == -1)
  1069.     PERROR (name);
  1070. }
  1071.  
  1072. #ifdef COFF
  1073. #ifndef COFF_BSD_SYMBOLS
  1074.  
  1075. /*
  1076.  *    If the COFF file contains a symbol table and a line number section,
  1077.  *    then any auxiliary entries that have values for x_lnnoptr must
  1078.  *    be adjusted by the amount that the line number section has moved
  1079.  *    in the file (bias computed in make_hdr).  The #@$%&* designers of
  1080.  *    the auxiliary entry structures used the absolute file offsets for
  1081.  *    the line number entry rather than an offset from the start of the
  1082.  *    line number section!
  1083.  *
  1084.  *    When I figure out how to scan through the symbol table and pick out
  1085.  *    the auxiliary entries that need adjustment, this routine will
  1086.  *    be fixed.  As it is now, all such entries are wrong and sdb
  1087.  *    will complain.   Fred Fish, UniSoft Systems Inc.
  1088.  */
  1089.  
  1090. /* This function is probably very slow.  Instead of reopening the new
  1091.    file for input and output it should copy from the old to the new
  1092.    using the two descriptors already open (WRITEDESC and READDESC).
  1093.    Instead of reading one small structure at a time it should use
  1094.    a reasonable size buffer.  But I don't have time to work on such
  1095.    things, so I am installing it as submitted to me.  -- RMS.  */
  1096.  
  1097. int
  1098. adjust_lnnoptrs (writedesc, readdesc, new_name)
  1099.      int writedesc;
  1100.      int readdesc;
  1101.      char *new_name;
  1102. {
  1103.   int nsyms;
  1104.   int new;
  1105. #if defined (amdahl_uts) || defined (pfa)
  1106.   SYMENT symentry;
  1107.   AUXENT auxentry;
  1108. #else
  1109.   struct syment symentry;
  1110.   union auxent auxentry;
  1111. #endif
  1112.  
  1113.   if (!lnnoptr || !f_hdr.f_symptr)
  1114.     return 0;
  1115.  
  1116. #ifdef MSDOS
  1117.   if ((new = writedesc) < 0)
  1118. #else
  1119.   if ((new = open (new_name, O_RDWR)) < 0)
  1120. #endif
  1121.     {
  1122.       PERROR (new_name);
  1123.       return -1;
  1124.     }
  1125.  
  1126.   lseek (new, f_hdr.f_symptr, 0);
  1127.   for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
  1128.     {
  1129.       read (new, &symentry, SYMESZ);
  1130.       if (symentry.n_numaux)
  1131.     {
  1132.       read (new, &auxentry, AUXESZ);
  1133.       nsyms++;
  1134.       if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400)
  1135.         {
  1136.           auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
  1137.           lseek (new, -AUXESZ, 1);
  1138.           write (new, &auxentry, AUXESZ);
  1139.         }
  1140.     }
  1141.     }
  1142. #ifndef MSDOS
  1143.   close (new);
  1144. #endif
  1145.   return 0;
  1146. }
  1147.  
  1148. #endif /* COFF_BSD_SYMBOLS */
  1149.  
  1150. #endif /* COFF */
  1151.  
  1152. #endif /* not CANNOT_UNEXEC */
  1153.  
  1154. #endif /* not CANNOT_DUMP */
  1155.